﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.Entities;
using System.Security.Claims;

namespace Sustainalytics.Claims.BL
{
    public class UserMapComparer : IUserMapComparer
    {
        public bool Diff(UserClaim leftUserMap, UserClaim rightUserMap, out bool diffAccount, out List<Claim> leftAdd, out List<Claim> rightAdd)
        {
            if (leftUserMap == null)
                throw new ArgumentException("Left userclaim should not be null!");

            if (rightUserMap == null)
                throw new ArgumentException("Right userclaim should not be null!");

            leftAdd = null;
            rightAdd = null;
            diffAccount = false;

            if (leftUserMap.Id != rightUserMap.Id || leftUserMap.Account != rightUserMap.Account)
            {
                diffAccount = true;
                return true;
            }

            return DiffClaims(leftUserMap.Claims, rightUserMap.Claims, out leftAdd, out rightAdd);
        }

        internal static bool DiffClaims(List<Claim> leftClaims, List<Claim> rightClaims, out List<Claim> leftAdd, out List<Claim> rightAdd)
        {
            var claimsComparer = new ExtendedClaimsComparer();
            leftAdd = leftClaims.Except(rightClaims, claimsComparer).ToList();
            rightAdd = rightClaims.Except(leftClaims, claimsComparer).ToList();

            return leftAdd.Count > 0 || rightAdd.Any();
        }
    }
}
